package net.gagnat.android.trackit.db;

import net.gagnat.android.trackit.util.TrackItConstants.LogEntryColumns;
import net.gagnat.android.trackit.util.TrackItConstants.LogTagColumns;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * This class helps open, create, and upgrade the database file.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String DATABASE_NAME = "log_it.db";
	public static final String LOG_ENTRY_TABLE_NAME = "logentry";
	public static final String LOG_TAG_TABLE_NAME = "logtag";
	public static final String LOG_ENTRY_TAG_LINK_TABLE_NAME = "logentrytaglink";
	public static final int DATABASE_VERSION = 1;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + LOG_TAG_TABLE_NAME + " (" //
				+ LogTagColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " //
				+ LogTagColumns.NAME + " TEXT" //
				+ ")");

		db.execSQL("CREATE TABLE " + LOG_ENTRY_TABLE_NAME + " (" //
				+ LogEntryColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " //
				+ LogEntryColumns.TITLE + " TEXT, " //
				+ LogEntryColumns.CREATED_DATE + " INTEGER, " //
				+ LogEntryColumns.TAGS + " TEXT" //
				+ ")"); //
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + LOG_TAG_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + LOG_ENTRY_TABLE_NAME);
		onCreate(db);
	}

}